home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1296 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  94 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: returning a string from a function
  5. Date: 12 Jan 1996 19:27:03 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4d6cm7$g4r@news.iag.net>
  8. References: <4d4uh8$q46@mailhost.mwmicro.com>
  9. NNTP-Posting-Host: pm1-orl25.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4d4uh8$q46@mailhost.mwmicro.com>, aschlies@citynet.net says...
  13. >
  14. >I am learning C with the aid of only a book.  I am at an inpass in
  15. >this process.  I have a small function that needs to return a string
  16. >back to the main routine. I have the following prototype:
  17. >
  18. >char function_name(char in_string[80])
  19. >
  20. >{
  21. >        char value[80];
  22. >
  23. >        ...value takes on part of the value of the last 10 characters
  24. >        of in_string.
  25. >
  26. >        return(value);
  27.  
  28. Your compiler should have complained about this.  Your function is defined
  29. to return a char, but you are trying to return a char pointer.
  30.  
  31. >}
  32. >
  33. >
  34. >jWhen this function is done, it only returns the first character.  I
  35. >"watched" the program execution and it shows that value indeed has the
  36. >last 10 characters.  Any clues as to why the calling routine only gets
  37. >the one character??
  38.  
  39. First, in c you can't pass or return an array (strings are '\0' terminated
  40. char arrays), only a pointer to an element in it (usually the first).  The
  41. c.l.c faq (Frequently Asked Question) list has a rather detailed discussion
  42. of this. You might want to read through it.  It is available for anonymous
  43. ftp from rtfm.mit.edu /pub/usenet/comp.lang.c.
  44.  
  45. If you change your function definition to :
  46.  
  47.    char *function_name(char in_string[80])
  48.  
  49. it will work (or seem to).  Unfortunately, you have just created another 
  50. problem.  value is local object with a storage type of automatic.  In
  51. other words, it is allocated (created) when the function is entered and
  52. deallocated (disappears), when the function exits.  So, by returning a 
  53. pointer to it, you are actually returning an invalid pointer (one that
  54. doesn't point to a valid object).
  55.  
  56. You can overcome this by defining value with a static storage class.
  57.  
  58.    static char value[80];
  59.  
  60. This means that there will be one permanent value object.  It will continue
  61. to exist, when the function exits.  However, each time you call function_name, 
  62. the prior contents of value will be overwritten.  
  63.  
  64. >
  65. >Also, I wrote the routine that copies the last 10 characters. Is there
  66. >a lib. function that does that for me?? Did I reinvent the wheel?
  67.  
  68. No, there is no standard function.  You simply set a pointer to the desired
  69. location in the string and strcpy the ptr.
  70.  
  71. #include <stdio.h>
  72. #include <string.h>
  73.  
  74. int main() 
  75.    {
  76.    char str1[] = "This is a nonsense string to demonstrate something.";
  77.    char buf[11], *ptr;
  78.    
  79.    if( strlen(str1) > 10)
  80.       ptr = str1 + (strlen(str1) - 10);
  81.    else
  82.       ptr = str1;
  83.       
  84.    strcpy( buf, ptr);
  85.    puts( buf);
  86.  
  87.    return 0;
  88.    }
  89.  
  90. -- 
  91. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  92. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  93.  
  94.